//
//  UIViewController+Alipay.m
//  CarJX
//
//  Created by jxcl on 14-9-25.
//  Copyright (c) 2014年 KK. All rights reserved.
//

#import "UIViewController+Alipay.h"
#import "Order.h"
#import "DataSigner.h"
#import <AlipaySDK/AlipaySDK.h>
#define Kpartner @"2088611360271630"
#define Kseller @"register@mail.clejw.com"
#define KprivateKey @"MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAOGE7ui7pUhB9PsYYH2HUFd7L0KSWtLZg1J77r5GA3veQQ7g5OzwEvhd1/D8VCeR+i6ZX/MgzlKkUAK6owvIHp2v1ce8s7TW8AGKJkmII4wrQbqqr4AtF4BTvf1vDkQyzuPsHJsJSY9WO9hgEhvx7ix6wozKAD3XxkHIUa7FYuUDAgMBAAECgYAEMdfLxmm9nVR0MzDgRNsOtYDciSJdV6RkrV90dJLuA+VL5Iz9hz2jFwPEPL1H+yQfJ8UsHYkY6mt+kYAk4PUIyt/9DwsN64D+qmG9cLPOpsw1Yvmg7DEnrfWAbdAXyjt8OuxQ4eiZEVbvrviGYeX82Xvubg/kdNUaROcNuyPi2QJBAP1l497l05MeWyerqEm1MLW3D/iDdfJcrRhv3e7SWJnN0wC14Y04uu5vHc7L+nUel3cudaGMb1lQGw0+UM6LJx8CQQDj1cIMZvvRl6bvhPUnfPvHQ6Uye91TSNFRUWbWcBXzdbvHy5G+9R0U2HwBKiAjesqtT6VVvxc28PwZF28k6jmdAkAushEM6VNU4BoyNErd0lr4kZSK2kbo87UeNLc0JGUZarmAfwPij1q7R5S5oBM2c/27g78/lu6KCd4CUcJ4XWJHAkEA2q9df7wFr4rpz/OFwG5l2UDjus5LhVpal7wTgna5iyMRp9eMwIbTcCPO+ustSADfs3TEVMaDiNfMkmT86KjtvQJAaLA+IM1FxIvnNuTEQgZ+09rphB54YTpbryU0s2/IYSpbidpDymqhU7FD1jWem+ptNE4rTkubtPh8tl9CI8oehA=="


@implementation Product
- (NSString *)description{
    return [NSString stringWithFormat:@"price : %d  , orderId : %@ ",self.price,self.orderId];
}
@end

@implementation UIViewController (Alipay)

#pragma mark -
#pragma mark   ==============产生随机订单号==============


- (NSString *)generateTradeNO
{
	static int kNumber = 15;
	
	NSString *sourceStr = @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	NSMutableString *resultStr = [[NSMutableString alloc] init];
	srand(time(0));
	for (int i = 0; i < kNumber; i++)
	{
		unsigned index = rand() % [sourceStr length];
		NSString *oneStr = [sourceStr substringWithRange:NSMakeRange(index, 1)];
		[resultStr appendString:oneStr];
	}
	return resultStr;
}



-(void)CreateOrderInfo:(Product *)product tradeNo:(NSString *)tradeNo callback:(CompletionBlock)completionBlock{
	/*
	 *生成订单信息及签名
	 */
	//将商品信息赋予AlixPayOrder的成员变量

    Order *order = [[Order alloc] init];
	order.partner = Kpartner;
	order.seller = Kseller;
	order.tradeNO = tradeNo; //订单ID（由商家自行制定）
	order.productName = product.subject; //商品标题
	order.productDescription = product.body; //商品描述
	order.amount = [NSString stringWithFormat:@"%.2f",product.price]; //商品价格
	order.notifyURL =  @"http://tapi.clejw.com/notify.jsp"; //回调URL

//    order.notifyURL =  @" http://192.168.1.163:8080/shopi/notify.jsp"; //回调URL
    order.service = @"mobile.securitypay.pay";
    order.paymentType = @"1";
    order.inputCharset = @"utf-8";
    order.itBPay = @"30m";
    order.showUrl = @"m.alipay.com";
	
	//应用注册scheme,在AlixPayDemo-Info.plist定义URL types
	NSString *appScheme = @"fhome";
	
	//将商品信息拼接成字符串
	NSString *orderSpec = [order description];
	NSLog(@"orderSpec = %@",orderSpec);
	
	//获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode
	id<DataSigner> signer = CreateRSADataSigner(KprivateKey);
	NSString *signedString = [signer signString:orderSpec];
	
	//将签名成功字符串格式化为订单字符串,请严格按照该格式
	NSString *orderString = nil;
	if (signedString != nil) {
		orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
                       orderSpec, signedString, @"RSA"];
        
        [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:completionBlock];
    }
}


@end
